<?php

namespace app\controllers;


use Yii;
use yii\web\Response;
use zonday\weixin\api;

class QrcodeController extends BaseController
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionGenerateTicket()
    {
        $qrcode = new api\Qrcode;
        $request = Yii::$app->getRequest();
        if ($request->isAjax && $request->isPost) {
            $expireSeconds = $request->post('expireSeconds');
            $actionName = $request->post('actionName');
            $sceneId = $request->post('sceneId');
            $sceneStr = $request->post('sceneStr');
            $result = $qrcode->create($expireSeconds, $actionName, $sceneId, $sceneStr);
            return new Response([
                'format' => Response::FORMAT_JSON,
                'data' => ['error' => 0, 'data' => $result],
            ]);
        }
    }

    public function actionQrcodeurl()
    {
        $qrcode = new api\Qrcode;
        $request = Yii::$app->getRequest();
        if ($request->isAjax && $request->isPost && $ticket = $request->post('ticket')) {
            $url = $qrcode->url($ticket);
            return new Response([
                'format' => Response::FORMAT_JSON,
                'data' => ['error' => 0, 'url' => $url],
            ]);
        }
    }
}